<template>
  <div>
    <ul>
      <!-- <li v-for="(message,index) in messages" :key="index"></li> -->
      <!-- <li v-for="(message,index) in messages" :key="message.id"></li> -->
      <!-- 上面两种的简化写法 -->
      <li v-for="message in messages" :key="message.id">
        <!-- <a href="???">{{message.title}}</a> -->
        <router-link :to="`/home/message/detail/${message.id}`">{{
          message.title
        }}</router-link>
        <button @click="pushShow(message.id)">push查看</button>
        <button @click="replaceShow(message.id)">replace查看</button>
      </li>
    </ul>
    <button @click="$router.back()">回退</button>
    <hr/>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  //数据
  data() {
    return {
      messages: [], //设置数据结构
    };
  },
  //每个组件对象只有一次mounted
  mounted() {
    //模拟Ajax请求从后台获取数据
    setTimeout(() => {
      const messages = [
        {
          id: 1,
          title: "message01", //
        },
        {
          id: 2,
          title: "message02", //
        },
        {
          id: 3,
          title: "message03", //
        },
        {
          id: 4,
          title: "message04", //
        },
      ];
      this.messages = messages;
    }, 1000);
  },
  //pushShow()、replaceShow()按钮点击事件
 /*  回退的顺序不同：replace():Home->news->message->replace查看
  点击浏览器左边箭头回退到news,而push回退到message
 */
  methods:{
    // 注意传的是id
    pushShow(id){
        this.$router.push(`/home/message/detail/${id}`);
    },
    replaceShow(id){
        this.$router.replace(`/home/message/detail/${id}`);
    }

  }
};
</script>

<style>
</style>